#include <signal.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gi18n.h>

#include "sig_handle.h"
#include "invoke_scripts.h"
#include "global_vars.h"

void njubras_interrupt(gint sig)
{
	disconnect_enable=FALSE;

	gtk_main_quit();
	bras_disconnect();

}

void dialog_response(gint sig)
{
	if (dialog_exist)
	{
		if (sig==SIGUSR2)
		{
			gtk_status_icon_set_from_file(connect_status_icon_global,"./image/online.png");
			disconnect_enable=TRUE;
			g_timeout_add(500,(GSourceFunc)(show_elapsed_time),NULL);
			gtk_dialog_response(GTK_DIALOG(dialog_global),GTK_RESPONSE_ACCEPT);
		}
		else
			gtk_dialog_response(GTK_DIALOG(dialog_global),GTK_RESPONSE_REJECT);
	}
}

gint show_elapsed_time()
{
	#define get_hours(t) (((guint)t)/3600)
	#define get_minutes(t) (((guint)t)/60)-((((guint)t)/3600)*60)
	#define get_seconds(t) (((guint)t)-(((guint)t)/60)*60)

	gdouble elapsed_time;
	GString *tooltip_text;

	elapsed_time=g_timer_elapsed(timer_global,NULL);
	tooltip_text=g_string_new("");
	g_string_printf(tooltip_text,_("Connected\nTime:%02u:%02u:%02u"),get_hours(elapsed_time),\
							get_minutes(elapsed_time),get_seconds(elapsed_time));
	gtk_status_icon_set_tooltip_text(connect_status_icon_global,tooltip_text->str);

	return disconnect_enable;
}
